<?php

/*
	This file is part of Mandragon.

    Mandragon is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Mandragon is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Mandragon.  If not, see <http://www.gnu.org/licenses/>.
*/

class EditSubPage {

	private $alerter;
	
	public function __construct() {
		$this->alerter = new Alerter();
	}

	/**
	 * adds extra access checks, dependent on page state
	 */
	function page_inc_check_access($page) {
		if (!has_access("MODERATE") and !has_access("ADMIN"))
			errorpage(5);
	}

	/**
	 * appropriate page action interpretations
	 */
	function page_inc_execute_action($action) {
		switch($action) {
			default:
				$this->alerter->add_alert("unknown action");
				break;
		}
	}

	/**
	 * execute page logic
	 */
	function page_inc_execute($page) {
		$creator = new FormCreator();
		$THISDIR = $page->directory_tree[$page->dir_depth-1];
		
		if (!$page->id)
			errorpage(9);
		
		$sub = db_fetch_array(db_do_query($page, db_query_select(array(array("body_id"), array("*")), array("SUBSET", "SUB_IN"), "dir_id = {$THISDIR['dir_id']} AND SUBSET.sub_id = SUB_IN.sub_id AND SUBSET.sub_id = {$page->id}")));

		if ($_POST['submitted']) {
		
			if ($_POST['delete'])
			
				if (db_num_rows(db_do_query($page, db_query_select(array(array("*")), array("ITEM_IN"), "sub_id = {$page->id} and deleted = 0"))))
				
					endpage("Deze subset bevat nog items, verwijder of verplaats die eerst voor je de subset zelf verwijdert");
			
				else if (!$_POST['confirmed']) {
				
					$page->content['form'] = $creator->confirm("Bent u zeker dat u deze subset wil verwijderen ?");
				
				} else if ($_POST['confirmed'] == "YES") {
				
					db_do_query($page, db_query_delete("SUBSET", "sub_id = {$page->id}"));
					db_do_query($page, db_query_delete("SUB_IN", "sub_id = {$page->id}"));
					
					redirect($page->dir_path);
				}
				
				
			$checker = new InputChecker();
			if ($checker->check_tags($_POST['title'], "title") && $checker->check_exists($_POST['title'], "title")) {
				db_do_query($page, db_query_update(array('sub_title'), array(db_sqlize($_POST['title'])), 
							"SUB_IN", "dir_id = {$THISDIR['dir_id']} AND sub_id = {$page->id}"));
			}
		
			redirect($page->dir_path . "/{$page->id}.subset");
		}
		
		$inputs = array();
		
		$inputs[] = $creator->input_field("Titel", "title", $new ? "" : db_desqlize($sub['sub_title']), "", 70);
		
		$inputs[] = $creator->input_checkbox("Subset verwijderen", "delete");
		
		$page->content['form'] = $creator->create("subform", array_reverse($inputs));
		
		$page->content['title'] = "Subset #{$page->id} aanpassen";
	}

	function generate_list($parent, $level, $keys, $vals, $menu) {
			
		$rank = 0;	
		$space = "";
		for ($i = 0; $i < $level; $i++)
			$space .= "&nbsp;&nbsp;&nbsp;&nbsp;";
		while ($id = @array_shift($menu[$parent]['children'])) {
			if ($menu[$id]['access'] != "DENY") {
				array_push($keys, "$id");
				array_push($vals, $space . $menu[$id]['menuname']);
				list($keys, $vals) = generate_list($id, ($level+1), $keys, $vals, $menu);
			}
		}	
		return array($keys, $vals);
	}

	/**
	 * dress up page content using skin stuff
	 */
	function page_inc_dress($page) {
		print_form($page, $page->content['form']);
	}
}

?>